package cn.duxiaod.demo;

import org.junit.Test;

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Java8新特性测试
 */
public class Java8Test {

    @Test
    public void streamTest(){
        List<String> strings = Arrays.asList("abc", "def", "gkh", "abc");
        //返回符合条件的stream
        Stream<String> stringStream = strings.stream().filter(s -> "abc".equals(s));
        //计算流复核条件流的数量
        long count = stringStream.count();
        System.out.println(count);
        System.out.println("---------------------");
        //foreach遍历->打印元素
        strings.forEach(System.out::println);
        System.out.println("---------------------");
        //排序后获取一个元素
        Stream<String> limit = strings.stream().sorted((a, b) -> b.compareTo(a)).limit(1);
        String[] strings1 = limit.toArray(String[]::new);
        System.out.println("排序后的第一个元素："+strings1[0]);
        System.out.println("---------------------");
        //对每个元素操作并排序打印
        strings.stream().map(s -> s + "1").sorted().forEach(System.out::println);
        System.out.println("---------------------");
        //Collector collect把指定元素放到容器
        List<String> collect = strings.stream().filter(s -> "abc".equals(s)).collect(Collectors.toList());
        collect.forEach(System.out::println);
        System.out.println("---------------------");
        String collect1 = strings.stream().collect(Collectors.joining(","));
        System.out.println(collect1);
        System.out.println("---------------------");
        //对数组的统计
        List<Integer> integers = Arrays.asList(1, 2, 5, 4, 3);
        IntSummaryStatistics statistics = integers.stream().mapToInt(s -> s).summaryStatistics();
        System.out.println("列表中最大的数 : "+statistics.getMax());
        System.out.println("列表中最小的数 : "+statistics.getMin());
        System.out.println("平均数 : "+statistics.getAverage());
        System.out.println("所有数之和 : "+statistics.getSum());
        System.out.println("---------------------");
        List<String> strings2 = Arrays.asList("xyz", "ccc");
        long count1 = Stream.concat(strings2.stream(), strings.stream()).count();
        System.out.println(count1);
        System.out.println("---------------------");
        //流只能使用一次，不能断开
        Stream<String> stream = strings.stream();
        stream.limit(2);
        //报错：java.lang.IllegalStateException: stream has already been operated upon or closed
        //stream.limit(3);
        System.out.println("---------------------");
        //但是可以这样, 连续使用
        Stream<String> stream2 = strings.stream();
        stream2.limit(2).forEach(System.out::println);
    }

    @Test
    public void laziness(){
        //在执行返回Stream方法时，并不是立刻执行，而是等处理成一个常规类型才会执行
        List<String> strings = Arrays.asList("abc", "def", "gkh", "abc");
        Stream<String> stream = strings.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                System.out.println("test");
                return false;
            }
        });
        System.out.println("count 执行");
        stream.count();
    }

    //并行流测试
    @Test
    public void parallelStreamTest(){
        List<Integer> integers = Arrays.asList(1, 2, 5, 4);
        integers.parallelStream().forEach(num-> System.out.println(Thread.currentThread().getName()+">>"+num));
    }


}
